﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using Public;

namespace QLNhanKhau
{
    public class DungChung
    {
        public static int LoadMaNK()
        {
            //NhanKhauPublic nk = NhanKhauBLL.LoadMaNK();
            ThietLapPublic tl = ThietLapBLL.LoadThietLap();

            //MessageBox.Show(frmDangNhap.user_login.HoTen.ToString());

            //tạo mank va mashk, lay ma cu lon nhat cong them 1
            int mank = NhanKhauBLL.LoadMaNK() + 1;
           
            string chuoiMaNK = mank.ToString();

            if (tl.MaPX < 10)
            {
                if (chuoiMaNK.Length < 6)
                    chuoiMaNK = "0" + tl.MaPX + "00000" + mank.ToString();
            }
            else
            {
                if (chuoiMaNK.Length < 6)
                    chuoiMaNK = tl.MaPX + mank.ToString();
            }
            return int.Parse(chuoiMaNK);
        }
        public static int LoadMaSHK()
        {
            //SoHoKhauPublic shk = SoHoKhauBLL.LoadMaSHK();
            ThietLapPublic tl = ThietLapBLL.LoadThietLap();

            //tạo mank va mashk, lay ma cu lon nhat cong them 1
            int mashk = SoHoKhauBLL.LoadMaSHK() + 1;
            string chuoiMaSHK = mashk.ToString(); ;

            if (tl.MaPX < 10)
            {
                if (mashk.ToString().Length < 6)
                    chuoiMaSHK = "0" + tl.MaPX + "00000" + mashk.ToString();
            }
            else
            {
                if (mashk.ToString().Length < 6)
                    chuoiMaSHK = tl.MaPX + mashk.ToString();

            }
            return int.Parse(chuoiMaSHK);
        }
        public static int SoChuyenKhau()
        {
            ThietLapPublic tl = ThietLapBLL.LoadThietLap();
            int sck = ChuyenKhauBLL.LoadMaCDi() + 1;
            string chuoiMaSCK = sck.ToString(); ;

            if (tl.MaPX < 10)
            {
                if (sck.ToString().Length < 6)
                    chuoiMaSCK = "0" + tl.MaPX + "00000" + sck.ToString();
            }
            else
            {
                if (sck.ToString().Length < 6)
                    chuoiMaSCK = tl.MaPX + sck.ToString();

            }
            return int.Parse(chuoiMaSCK);
        }
        public static byte[] encryptData(string data)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
            return hashedBytes;
        }
        public static string md5(string data)
        {
            return BitConverter.ToString(encryptData(data)).Replace("-", "").ToLower();
        }
        public static int LoadMaND()
        {
            ThietLapPublic tl = ThietLapBLL.LoadThietLap();
            int nd = NguoiDungBLL.LoadMaND() + 1;
            string chuoiMaND = nd.ToString(); ;

            if (tl.MaPX < 10)
            {
                if (nd.ToString().Length < 6)
                    chuoiMaND = "0" + tl.MaPX + "00000" + nd.ToString();
            }
            else
            {
                if (nd.ToString().Length < 6)
                    chuoiMaND = tl.MaPX + nd.ToString();

            }
            return int.Parse(chuoiMaND);
        }
        public static int LoadMaCDi()
        {
            ThietLapPublic tl = ThietLapBLL.LoadThietLap();
            int cdi = ChuyenKhauBLL.LoadMaCDi() + 1;
            string chuoiMaCDi = cdi.ToString(); ;

            if (tl.MaPX < 10)
            {
                if (cdi.ToString().Length < 6)
                    chuoiMaCDi = "0" + tl.MaPX + "00000" + cdi.ToString();
            }
            else
            {
                if (cdi.ToString().Length < 6)
                    chuoiMaCDi = tl.MaPX + cdi.ToString();

            }
            return int.Parse(chuoiMaCDi);
        }
    }
}
